package GUI.TOOL;
/**
 *
 * @author Khôi
 */
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.text.TabExpander;

public class InHoaDon {

	public static void MainClass(TableModel dauTrang, TableModel thanTrang, TableModel cuoiTrang) {
		PrinterJob pj = PrinterJob.getPrinterJob();

		PageFormat pf = pj.defaultPage();
		Paper paper = new Paper();
		double margin = 36; // half inch
		paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
		pf.setPaper(paper);
		MyPrintable tmp = new MyPrintable();
		tmp.setDauTrang(dauTrang);
		tmp.setThanTrang(thanTrang);
		tmp.setCuoiTrang(cuoiTrang);
		pj.setPrintable(tmp, pf);
		if (pj.printDialog()) {

			try {

				pj.print();
			} catch (PrinterException e) {
				System.out.println(e);
			}
		}
	}
}

class MyPrintable implements Printable {

	private TableModel dauTrang;
	private TableModel thanTrang;
	private TableModel cuoiTrang;

	public void setDauTrang(TableModel dauTrang) {
		this.dauTrang = dauTrang;
	}

	public void setThanTrang(TableModel thanTrang) {
		this.thanTrang = thanTrang;
	}

	public void setCuoiTrang(TableModel cuoiTrang) {
		this.cuoiTrang = cuoiTrang;
	}

	public int print(Graphics g, PageFormat pf, int pageIndex) {
		if (pageIndex != 0) {
			return NO_SUCH_PAGE;
		}

		Graphics2D g2 = (Graphics2D) g;
		g2.setFont(new Font("Serif", Font.PLAIN, 26));
		g2.setPaint(Color.black);
		g2.drawString("HÓA ĐƠN BÁN HÀNG", 150, 70);
		g2.setFont(new Font("Serif", Font.PLAIN, 14));

		//dau trang
		int KhoangCachSoVoiDauTrang = 0;
		for (int i = 0; i < this.dauTrang.getRowCount(); i++) {
			for (int j = 0; j < this.dauTrang.getColumnCount(); j++) {
				int khoangCachSoVoiDongTruoc = 14;

				int x, y = 0;
				x = 80 + 150 * j;
				y = 120 + i * khoangCachSoVoiDongTruoc;
				KhoangCachSoVoiDauTrang = y;
				//	g2.drawString(str, chieu  ngang , chieu doc )

				Object ob = dauTrang.getValueAt(i, j);
				g2.drawString(ob.toString(), x, y);
			}
		}

		// than trang 
		for (int i = 0; i < this.thanTrang.getColumnCount(); i++) {
			String s = this.thanTrang.getColumnName(i).toString();
			int x = 0;
			x = 80 + 80 * i;
			if (i == 1 || i == 2) {
				x -= 50;
			}

			g2.drawString(s, x, KhoangCachSoVoiDauTrang + 40);

		}
		g2.setFont(new Font("Serif", Font.PLAIN, 12));

		int khoangCachSoVoiDongTruoc = 14;
		int KCM = 0;
		for (int i = 0; i < this.thanTrang.getRowCount(); i++) {
			for (int j = 0; j < this.thanTrang.getColumnCount(); j++) {
				int x = 0;
				x = 80 + 80 * j;
				if (j == 1 || j == 2) {
					x -= 50;

				}
				
				//	g2.drawString(str, chieu  ngang , chieu doc )
				Object ob = thanTrang.getValueAt(i, j);
				KCM = KhoangCachSoVoiDauTrang + 60 + i * khoangCachSoVoiDongTruoc;
				g2.drawString(ob.toString(), x, KCM);
				// trang moi thi su dung cai nay 
				//g2.drawString(ob.toString(), x, KhoangCachSoVoiDauTrang+ 120 + i * khoangCachSoVoiDongTruoc);
			}

		}
		// CUOI TRANG 
		g2.setFont(new Font("Serif", Font.PLAIN, 14));

		for (int i = 0; i < this.cuoiTrang.getRowCount(); i++) {
			for (int j = 0; j < this.cuoiTrang.getColumnCount(); j++) {

				int x, y = 0;
				x = 80 + 150 * j;
				y =  40+i * khoangCachSoVoiDongTruoc;

				//	g2.drawString(str, chieu  ngang , chieu doc )

				Object ob = cuoiTrang.getValueAt(i, j);
				g2.drawString(ob.toString(), x, y + KCM);
			}
		}

		Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf.getImageableWidth(), pf.getImageableHeight());
		g2.draw(outline);
		return PAGE_EXISTS;
	}
}